/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Componenten.LocalTypes;
import Model.Bord;
import Model.Element;
import Model.Vak;
import Model.Level;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;

/**
 *
 * @author Bram Slob en Roderick van Seijen
 */
public class BordView extends JPanel implements LocalTypes, KeyListener{
    private final int vakGrootte = 36;
    private Bord bord;
    private Sokoban sokoban;
    
    public BordView(Sokoban sokoban)
    {
        bord = new Bord();
        //bord.initLevel(1);
        setFocusable(true);
        setPreferredSize(new Dimension(Level.getAantalKolommen() * vakGrootte, Level.getAantalRijen() * vakGrootte));
        addKeyListener(this);
        this.sokoban = sokoban;
    }
    
    @Override
    public void keyTyped ( KeyEvent e ){  
        
    }   
    @Override
    public void keyPressed ( KeyEvent e){  
        int code = e.getKeyCode();
        switch (code) {
            case KeyEvent.VK_KP_LEFT:
            case KeyEvent.VK_LEFT:
                bord.beweegSpeler(Richting.WEST);
                break;
            case KeyEvent.VK_KP_DOWN:
            case KeyEvent.VK_DOWN:
                bord.beweegSpeler(Richting.ZUID);
                break;
            case KeyEvent.VK_KP_RIGHT:
            case KeyEvent.VK_RIGHT:
                bord.beweegSpeler(Richting.OOST);
                break;
            case KeyEvent.VK_KP_UP:
            case KeyEvent.VK_UP:
                bord.beweegSpeler(Richting.NOORD);
                break;
        }
        sokoban.updateScore(bord.getScore());
        repaint();
    }  
    @Override
    public void keyReleased ( KeyEvent e ){  
        
    }
  
    private void bouwWereld(Graphics g) {
       
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(new Color(250, 240, 170));
        
        Vak[][] vakMatrix = bord.getVakMatrix();
        Vak vk = null;
        Element el = null;
        
        for(int i = 0; i < Level.getAantalRijen() ; i++){
            for(int j = 0; j < Level.getAantalKolommen(); j++){
                vk = vakMatrix[i][j];
                if(vk != null){
                    g.drawImage(vk.getAfbeelding(), ( j * vakGrootte), ( i * vakGrootte), this);
                    el = vk.getElement();
                    if (el != null) {
                        g.drawImage(el.getAfbeelding(), ( j * vakGrootte), ( i * vakGrootte), this);
                    }
                }
            }        
        }
    }
    
    @Override
    public void paint(Graphics g) {
        System.out.println("yup3");
        super.paint(g);
        if(bord.getLevelCode() > 0){
            bouwWereld(g);
            System.out.println("yup2");
        }else{
            System.out.println("nope1");
        }
        
    }
    
    public Sokoban getSokoban() {
        return sokoban;
    }

    public void setSokoban(Sokoban sokoban) {
        this.sokoban = sokoban;
    }
    
    public void startLevel(){
        System.out.println("start");
        sokoban.updateScore(0);
        bord.initLevel(1);
        System.out.println(bord.getLevelCode() + " ");
        this.repaint();
    }
    
    public void herstartLevel(){
        sokoban.updateScore(0); 
        bord.reinitLevel();
        repaint();
    }
}
